package com.hph.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hph.health.common.BaseResponse;
import com.hph.health.common.ErrorCode;
import com.hph.health.common.ResultUtils;
import com.hph.health.exception.BusinessException;
import com.hph.health.exception.ThrowUtils;
import com.hph.health.model.dto.order.AddOrderRequest;
import com.hph.health.model.dto.user.DeleteRequest;
import com.hph.health.model.entity.Order;
import com.hph.health.model.entity.SocialShare;
import com.hph.health.service.SocialShareService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author hanph
 * @from haut
 */
@RestController
@RequestMapping("/share")
@Slf4j
public class SocialShareController {

    @Resource
    private SocialShareService socialShareService;

    /**
     * 获取分享列表
     *
     * @return
     */
    @PostMapping("/list")
    public BaseResponse<List<SocialShare>> listSocialShare() {
        QueryWrapper<SocialShare> queryWrapper = new QueryWrapper<>();
        List<SocialShare> list = socialShareService.list(queryWrapper);
        return ResultUtils.success(list);
    }

    /**
     *  新增分享
     *
     * @param socialShare
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addShare(@RequestBody SocialShare socialShare) {
        if (socialShare == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = socialShareService.save(socialShare);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(socialShare.getId());
    }
    /**
     * 删除分享
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteShare(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(socialShareService.removeById(deleteRequest.getId()));
    }

}
